#!/bin/sh
#/*--------------------------------------------------------------------------
# *  Copyright 2011 Taro L. Saito
# *
# *  Licensed under the Apache License, Version 2.0 (the "License");
# *  you may not use this file except in compliance with the License.
# *  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# *  Unless required by applicable law or agreed to in writing, software
# *  distributed under the License is distributed on an "AS IS" BASIS,
# *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# *  See the License for the specific language governing permissions and
# *  limitations under the License.
# *--------------------------------------------------------------------------*/
#//--------------------------------------
#// XerialJ
#//
#// gw: Genome Weaver 
#// Since: 2011/02/04
#//
#//--------------------------------------

if [ -z "$GENOME_WEAVER_HOME" ] ; then
  ## resolve links - $0 may be a link to GENOME_WEAVER home
  PRG="$0"

  # need this for relative symlinks
  while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG="`dirname "$PRG"`/$link"
    fi
  done

  saveddir=`pwd`

  GENOME_WEAVER_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  GENOME_WEAVER_HOME=`cd "$GENOME_WEAVER_HOME" && pwd`

  cd "$saveddir"
fi

cygwin=false
mingw=false
darwin=false
case "`uname`" in
  CYGWIN*) cygwin=true;;
  MINGW*) mingw=true;;
  Darwin*) darwin=true
           if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
            echo "Using Java version: $JAVA_VERSION" 1>&2 
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
           fi
           JVM_OPT="$JVM_OPT -Xdock:name="Genome Weaver" -Xdock:icon=$GENOME_WEAVER_HOME/GENOME_WEAVER-icon-mac.png -Dcom.apple.macos.useScreenMenuBar=true"
           JAVACMD="`which java`"
           ;;
esac

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD="`which java`"
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly." 1>&2
  echo "  We cannot execute $JAVACMD" 1>&2
  exit 1
fi

#if [ -z "$JAVA_HOME" ] ; then
#  echo "Warning: JAVA_HOME environment variable is not set." 1>&2
#fi


# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  [ -n "$GENOME_WEAVER_HOME" ] &&
    GENOME_WEAVER_HOME=`cygpath --path --windows "$GENOME_WEAVER_HOME"`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
#  [ -n "$HOME" ] &&
#    HOME=`cygpath --path --windows "$HOME"`
fi

# For Migwn, ensure paths are in UNIX format before anything is touched
if $mingw ; then
  [ -n "$GENOME_WEAVER_HOME" ] &&
    GENOME_WEAVER_HOME="`(cd "$GENOME_WEAVER_HOME"; pwd)`"
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
  # TODO classpath?
fi

CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher

exec "$JAVACMD" -Dlog.color=true ${JVM_OPT} \
  -classpath "${GENOME_WEAVER_HOME}"/boot/plexus-classworlds-*.jar \
  "-Dclassworlds.conf=${GENOME_WEAVER_HOME}/bin/gw.conf" \
  "-Dgw.home=${GENOME_WEAVER_HOME}" \
  ${CLASSWORLDS_LAUNCHER} "$@"


